package com.zhiwang.xiaoqu.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.zhiwang.xiaoqu.R

class BoringItem : FrameLayout {


    var bottomLineMargin: Int = 0

    var bottomLineMarginViewId: Int = 0

    var leftIcon: Bitmap
    var navIcon: Bitmap


    var text: String = ""

    var textMargin: Int = 0

    lateinit var bottomLineMarginView: View

    @ColorInt
    var lineColor: Int = Color.TRANSPARENT

    var lineThickness: Int = 0

    var paint = Paint()

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {


        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.BoringItem)


        text = typedArray.getString(R.styleable.BoringItem_boring_text) ?: ""

        textMargin = typedArray.getDimensionPixelSize(R.styleable.BoringItem_text_margin, 0)

        bottomLineMargin = typedArray.getDimensionPixelSize(R.styleable.BoringItem_line_marginStart, 0)

        bottomLineMarginViewId = typedArray.getResourceId(R.styleable.BoringItem_margin_view_id, 0)

        lineThickness = typedArray.getDimensionPixelSize(R.styleable.BoringItem_line_thickness, 2)

        lineColor = typedArray.getColor(
            R.styleable.BoringItem_line_color,
            ContextCompat.getColor(context, R.color.colorPrimary)
        )
        paint.color = lineColor

        leftIcon =
            BitmapFactory.decodeResource(
                context.resources,
                typedArray.getResourceId(R.styleable.BoringItem_left_icon, 0)
            )

        navIcon =
            BitmapFactory.decodeResource(
                context.resources,
                typedArray.getResourceId(R.styleable.BoringItem_nav_icon, 0)
            )

        val imageView = ImageView(context)

        var lp = LayoutParams(-2, -2)
        lp.gravity = Gravity.CENTER_VERTICAL or Gravity.START
        imageView.setImageBitmap(leftIcon)
        addView(imageView, lp)


        val textView = TextView(context)
        textView.text = text

        lp = LayoutParams(-2, -2)
        lp.leftMargin = leftIcon.width + textMargin
        lp.gravity = Gravity.CENTER_VERTICAL

        addView(textView, lp)


        lp = LayoutParams(-2, -2)
        lp.gravity = Gravity.CENTER_VERTICAL or Gravity.END
        val navImageView = ImageView(context)



        navImageView.setImageBitmap(navIcon)

        addView(navImageView, lp)

        typedArray.recycle()
    }


    override fun onFinishInflate() {
        super.onFinishInflate()
        //同时设置了 viewid 和 margin，以margin 为准,否则参考viewid

        if (bottomLineMarginViewId != 0 && bottomLineMargin == 0) {
            bottomLineMarginView = findViewById(bottomLineMarginViewId)
            bottomLineMargin = bottomLineMarginView.measuredWidth + Math.max(paddingStart, paddingLeft)
        }
    }


    override fun dispatchDraw(canvas: Canvas?) {
        //下划线
        canvas?.drawLine(
            bottomLineMargin.toFloat(),
            measuredHeight.toFloat() - lineThickness / 2f,
            measuredWidth.toFloat(),
            measuredHeight.toFloat() - lineThickness / 2f,
            paint
        )
        super.dispatchDraw(canvas)
    }
}